package com.library.view.components;

import com.library.utils.StringUtils;
import io.yukk1o.easytermui.base.BasePanel;
import io.yukk1o.easytermui.base.Listener.InteractiveListener;
import io.yukk1o.easytermui.component.Button;
import io.yukk1o.easytermui.component.Text;

/**
 * 通用提示弹窗
 *
 * 包含组件:
 * 弹窗内：提示文本（显示操作结果/原因，如成功、失败提示）、确认按钮
 *
 */
public class ConfirmDialog extends BasePanel {
    private int resultCode;


    /**
     * 构造「嵌套Panel」（相对坐标）
     *
     * @param relX   相对于父Panel的X坐标
     * @param relY   相对于父Panel的Y坐标
     */
    public ConfirmDialog(int relX, int relY, String title) {
        super(relX, relY, 40, 7);
        init(title);
    }

    public void init(String title){
        /// 获取居中文本起始位置(相对位置)
        int centerX = (getWidth() - StringUtils.getDisplayLength(title)) / 2;
        Text titleText = new Text(centerX, 1, title.length(), 2, title);
        addComponent(titleText);

        Button cancelBtn = new Button(5, 4, " 取 消 ", (button -> {
            this.resultCode = 0;
            InteractiveListener listener = (InteractiveListener) getBindData();
            listener.stopListener();
        }));
        addComponent(cancelBtn);

        Button confirmBtn = new Button(25, 4, " 确 定 ", (button -> {
            this.resultCode = 1;
            InteractiveListener listener = (InteractiveListener) getBindData();
            listener.stopListener();
        }));
        addComponent(confirmBtn);
    }

    @Override
    public void renderContext() {
        drawBorder();
        super.renderContext();
    }

    public int getResultCode() {
        return resultCode;
    }
}
